import { isEmpty } from './types'

/**
 * 数字千分位转换
 */
export const thousandsNumber = number => {
  if (isEmpty(number)) return ''
  if (isNaN(Number(number))) return ''

  // 转为字符串，并按照.拆分
  const arr = (number + '').split('.')
  // 整数部分再拆分
  const int = arr[0].split('')
  // 小数部分
  const fraction = arr[1] || ''
  // 返回的变量
  let r = ''
  int.reverse().forEach(function (v, i) {
    // 非第一位并且是位值是3的倍数，添加“,”
    if (i !== 0 && i % 3 === 0) {
      r = v + ',' + r
    } else {
      // 正常添加字符(这是好写法)
      r = v + r
    }
  })
  // 整数部分和小数部分拼接
  return r + (fraction ? '.' + fraction : '')
}

/**
 * 格式化数量 （默认展示千分位，大于1万展示1w，大于1亿展示1亿，保留两位小数）
 */
export const formatNumber = (
  value,
  { thousands = true, suffix = 'w', precision = undefined, limitPrecision = 2, zero = '0' } = {}
) => {
  if (isEmpty(value)) return ''
  let num = Number(value)
  if (isNaN(num)) return ''
  // 数值最小值，代表无
  if (num === -2147483648) return ''

  if (num === 0) {
    return zero
  }
  const prefix = num < 0 ? '-' : ''
  num = Math.abs(num)
  if (num < 10000) {
    const count = precision || precision === 0 ? num.toFixed(precision) : num
    return prefix + (thousands ? thousandsNumber(count) : `${count}`)
  }

  if (num >= 100000000) {
    const count = (num / 100000000).toFixed(limitPrecision)
    return prefix + (thousands ? `${thousandsNumber(count)}${'亿'}` : `${count}${'亿'}`)
  }

  const count = (num / 10000).toFixed(limitPrecision)
  return prefix + (thousands ? `${thousandsNumber(count)}${suffix}` : `${count}${suffix}`)
}

/**
 * Tooltip 展示完整数字（小于10000则不展示tooltip）
 */
export const formatNumberTooltip = (
  value,
  { thousands = true, limit = 10000, precision = undefined } = {}
) => {
  if (isEmpty(value)) return ''
  const num = Number(value)
  if (isNaN(num)) return ''
  if (num < limit) return ''

  const count = precision || precision === 0 ? num.toFixed(precision) : num
  return thousands ? thousandsNumber(count) : `${count}`
}

/**
 * 格式化时长 （秒数转为xx分xx秒，最多至xx天）
 */
export const formatDuration = seconds => {
  if (isEmpty(seconds)) return ''

  // 获取dayjs时长对象
  const d = dayjs.duration(seconds, 's')
  const durationList = []

  if (d.days() > 0) durationList.push(`${d.days()}天`)
  if (d.hours() > 0) durationList.push(`${d.hours()}小时`)
  if (d.minutes() > 0) durationList.push(`${d.minutes()}分`)
  durationList.push(`${d.seconds()}秒`)

  return durationList.join('')
}

/**
 * 格式化时间 刚刚/x分钟前/x小时前/2022-05-11 11:11:11
 */
export const formatTimeForNow = secondTimeStamp => {
  if (isEmpty(secondTimeStamp) || secondTimeStamp == 0) return ''

  const secondTimeStampNum = Number(secondTimeStamp)

  // 时间距离现在的秒数
  const seconds = parseInt(dayjs().valueOf() / 1000, 10) - secondTimeStampNum

  if (seconds < 60) return '刚刚'
  if (seconds < 60 * 60) return `${parseInt(seconds / 60, 10)}分钟前`
  if (seconds < 60 * 60 * 24) return `${parseInt(seconds / (60 * 60), 10)}小时前`
  return dayjs(secondTimeStampNum * 1000).format('YYYY-MM-DD HH:mm:ss')
}

/**
 * 格式化 抖音头像替换为无需签名的头像
 */
export const formatSignDyAvatar = src => {
  if (isEmpty(src)) return ''
  const matchStr = src.match(/\/\/.+-sign.+\.com/)?.[0]
  if (!matchStr) return ''

  return src.replace(matchStr, '//p3-pc.douyinpic.com').replace('200x200', '100x100').split('?')[0]
}
